import * as fs from 'fs';
import * as path from 'path';

/**
 * File Storage Utility
 * 
 * Provides file-based storage operations for projects and other data.
 */

const WORKSPACES_DIR = path.join(__dirname, '../../../workspaces');

/**
 * Ensures the workspaces directory exists
 */
export function ensureWorkspacesDir(): void {
  if (!fs.existsSync(WORKSPACES_DIR)) {
    fs.mkdirSync(WORKSPACES_DIR, { recursive: true });
  }
}

/**
 * Gets the path to a project directory
 * @param projectId - The project ID
 * @returns string - The path to the project directory
 */
export function getProjectPath(projectId: string): string {
  return path.join(WORKSPACES_DIR, projectId);
}

/**
 * Checks if a project exists
 * @param projectId - The project ID
 * @returns boolean - Whether the project exists
 */
export function projectExists(projectId: string): boolean {
  return fs.existsSync(getProjectPath(projectId));
}

/**
 * Reads a project file
 * @param projectId - The project ID
 * @param fileName - The name of the file to read
 * @returns any - The parsed JSON data or null if not found
 */
export function readProjectFile(projectId: string, fileName: string): any {
  try {
    const projectPath = getProjectPath(projectId);
    const filePath = path.join(projectPath, fileName);
    
    if (!fs.existsSync(filePath)) {
      return null;
    }
    
    const data = fs.readFileSync(filePath, 'utf8');
    return JSON.parse(data);
  } catch (error) {
    console.error(`Error reading project file ${fileName}:`, error);
    return null;
  }
}

/**
 * Writes a project file
 * @param projectId - The project ID
 * @param fileName - The name of the file to write
 * @param data - The data to write
 */
export function writeProjectFile(projectId: string, fileName: string, data: any): void {
  try {
    const projectPath = getProjectPath(projectId);
    
    // Ensure project directory exists
    if (!fs.existsSync(projectPath)) {
      fs.mkdirSync(projectPath, { recursive: true });
    }
    
    const filePath = path.join(projectPath, fileName);
    fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
  } catch (error) {
    console.error(`Error writing project file ${fileName}:`, error);
  }
}

/**
 * Lists all projects in the workspaces directory
 * @returns string[] - Array of project IDs
 */
export function listProjects(): string[] {
  try {
    ensureWorkspacesDir();
    
    if (!fs.existsSync(WORKSPACES_DIR)) {
      return [];
    }
    
    return fs.readdirSync(WORKSPACES_DIR).filter(item => {
      const itemPath = path.join(WORKSPACES_DIR, item);
      return fs.statSync(itemPath).isDirectory();
    });
  } catch (error) {
    console.error('Error listing projects:', error);
    return [];
  }
}

/**
 * Deletes a project
 * @param projectId - The project ID
 */
export function deleteProject(projectId: string): void {
  try {
    const projectPath = getProjectPath(projectId);
    
    if (fs.existsSync(projectPath)) {
      fs.rmSync(projectPath, { recursive: true });
    }
  } catch (error) {
    console.error(`Error deleting project ${projectId}:`, error);
  }
}